<?php
/**
 * Created by PhpStorm.
 * User: giozola
 * Date: 2018/11/16
 * Time: 17:31
 */

namespace addons\hlapp\model;


use addons\hlapp\controller\FileCache;

class HlappTypeStage extends CommonModel
{
    public function type()
    {
        return $this->belongsTo('HlappProjectType', 'type_id');
    }

    /**
     * 获取评价方面
     * @param $stageId
     * @return null
     */
    public static function getAspect($stageId){
        $stage = self::getStageById($stageId);
        if ($stage){
            $name = $stage['aspect'];
        }else{
            $name = null;
        }
        return $name;
    }

    /**
     * 获取阶段名称
     * @param $stageId
     * @return null
     */
    public static function getStageName($stageId){
        $stage = self::getStageById($stageId);
        if ($stage){
            $name = $stage['name'];
        }else{
            $name = null;
        }
        return $name;
    }

    /**
     * 获取阶段对象
     * @param $stageId
     * @return mixed
     */
    public static function getStageById($stageId)
    {
        $stages = self::getTypeStage();
        if (array_key_exists($stageId,$stages)){
            $stage = $stages[$stageId];
        }else{
            $stage = null;
        }
        return $stage;
    }

    /**
     * 获取项目一下阶段
     * 最后阶段返回 null
     * @param $typeId
     * @return mixed|null
     */
    public static function getNextStage($typeId, $stageId=null)
    {
        //获取种类所有阶段
        $stageList = self::getStageListByType($typeId);
        // 下一阶段
        if ($stageId){
            $stages = self::getTypeStage();
            $sort = $stages[$stageId]['sort'];
            $sort = $sort + 1;
        }else{
            //默认阶段
            $sort = 1;
        }
        foreach ($stageList as $key => $value) {
            if ($value['sort'] == $sort) {
                return $value;
            }
        }
        return null;
    }

    /**
     * 获取某种类所有阶段
     */
    public static function getStageListByType($typeId)
    {
        $typeStageInfo = self::getTypeStage();
        $stageList = array();
        foreach ($typeStageInfo as $key => $value) {
            if ($value['type_id'] == $typeId) {
                array_push($stageList, $value);
            }
        }
        return $stageList;
    }

    /**
     * 获取项目种类阶段缓存
     */
    public static function getTypeStage()
    {
        $type_stage_cache = CacheEnum::TYPE_STAGE_CACHE;
        $cache = new FileCache();
        $typeStageList = $cache->get($type_stage_cache);
        if (!$typeStageList) {
            \Log::INFO("更新项目阶段缓存");
            $typeStageTemp = HlappTypeStage::all();
            foreach ($typeStageTemp as $val) {
                $typeStageList[$val['id']] = $val;
            }
            $cache->set($type_stage_cache, $typeStageList);
        }
        return $typeStageList;
    }
}